package com.jsgt.greekfire.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.fhx.core.base.BaseViewModel
import com.jsgt.greekfire.bean.CoinListBean
import com.jsgt.greekfire.bean.MyOrderBean
import com.jsgt.greekfire.bean.MyUserAdBean
import com.jsgt.greekfire.bean.OtcAdBean
import com.jsgt.greekfire.common.http.ResponseData
import com.jsgt.greekfire.common.http.safeLaunch
import com.jsgt.greekfire.repository.CoinCurrencyRepository
import retrofit2.HttpException
import java.net.ConnectException
import java.net.SocketTimeoutException

class CoinCurrencyViewModel : BaseViewModel() {
    private val coinCurrencyRepository = CoinCurrencyRepository.instance

    val errorLiveData by lazy { MutableLiveData<Any>() }
    val coinlistData by lazy { MutableLiveData<ResponseData<List<CoinListBean>>>() }
    val otcAdData by lazy { MutableLiveData<ResponseData<OtcAdBean>>() }
    val otcAdStateData by lazy { MutableLiveData<Any>() }
    val AddOrderData by lazy { MutableLiveData<Any>() }
    val userOrderData by lazy { MutableLiveData<ResponseData<MyOrderBean>>() }
    val userAdData by lazy { MutableLiveData<ResponseData<MyUserAdBean>>() }
    val addAdData by lazy { MutableLiveData<Any>() }
    val adDownData by lazy { MutableLiveData<Any>() }

    private fun doError(error: Throwable) {
        if (error is SocketTimeoutException) {
            errorLiveData.value = "网络请求超时"
        } else if (error is ConnectException || error is HttpException) {
            errorLiveData.value = "网络请求失败"
        } else {
            errorLiveData.value = error.message
        }
    }


    /**
     * 获取币种列表
     */
    fun getCoinlistData() {
        viewModelScope.safeLaunch {
            block = {
                val datas = coinCurrencyRepository.getCoinList()
                if (datas.code == 0) {
                    coinlistData.value = datas;
                } else {
                    errorLiveData.value = datas;
                }
            }
            onError = {
//                errorLiveData.value = it.message;
                doError(it)
            }
        }
    }
    /**
     * 获取币种列表
     */
    fun getotcAdData(dealType: Int,dealState: Int,page: Int) {
        val params = HashMap<String, Any>()
        params["dealType"] = dealType.toString()
        params["dealState"] = dealState.toString()
        params["page"] = page
        viewModelScope.safeLaunch {
            block = {
                val datas = coinCurrencyRepository.getotcAd(params)
                if (datas.code == 0) {
                    otcAdData.value = datas
                } else {
                    errorLiveData.value = datas;
                }
            }
            onError = {
                //errorLiveData.value = it.message;
                doError(it)
            }
        }
    }
    /**
     * 是否可以购买
     */
    fun getotcAdStateData(adId: String,state: String) {
        val params = HashMap<String, Any>()
        params["adId"] = adId
        params["state"] = state
        viewModelScope.safeLaunch {
            block = {
                val datas = coinCurrencyRepository.getotcAdState(params)
                if (datas.code == 0) {
                    otcAdStateData.value = datas.msg
                } else {
                    errorLiveData.value = datas.msg;
                }
            }
            onError = {
                //errorLiveData.value = it.message;
                doError(it)
            }
        }
    }
    /**
     * 出售
     */
    fun getAddOrder(dealNum: String,adId: String,payPassword: String) {
        val params = HashMap<String, Any>()
        params["dealNum"] = dealNum
        params["adId"] = adId
        params["payPassword"] = payPassword
        viewModelScope.safeLaunch {
            block = {
                val datas = coinCurrencyRepository.getAddOrder(params)
                if (datas.code == 0) {
                    AddOrderData.value = datas.msg
                } else {
                    errorLiveData.value = datas.msg;
                }
            }
            onError = {
                //errorLiveData.value = it.message;
                doError(it)
            }
        }
    }
    /**
     * 我的订单列表
     */
    fun getUserOrderData(page: Int) {
        val params = HashMap<String, Any>()
        params["page"] = page
        viewModelScope.safeLaunch {
            block = {
                val datas = coinCurrencyRepository.getUserOrder(params)
                if (datas.code == 0) {
                    userOrderData.value = datas
                } else {
                    errorLiveData.value = datas.msg;
                }
            }
            onError = {
                //errorLiveData.value = it.message;
                doError(it)
            }
        }
    }
    /**
     * 发布广告列表
     * dealState  0 显示取消订单 1不显示取消订单
     */
    fun getUserAd(page: Int,dealState: Int) {
        val params = HashMap<String, Any>()
        params["page"] = page
        params["dealState"] = dealState
        viewModelScope.safeLaunch {
            block = {
                val datas = coinCurrencyRepository.getUserAd(params)
                if (datas.code == 0) {
                    userAdData.value = datas
                } else {
                    errorLiveData.value = datas.msg;
                }
            }
            onError = {
                //errorLiveData.value = it.message;
                doError(it)
            }
        }
    }
    /**
     * 发布广告  "dealType":2,  类型 1卖出  2买入
    "dealNum":100, 数量
    "dealAmount":2.1, 价格
    "payPassword":"212117", 密码
    "coinCode":"GFC" 币种
     *
     */
    fun getAddAd(dealType: Int,dealNum: String,dealAmount: String,payPassword: String,coinCode: String) {
        val params = HashMap<String, Any>()
        params["dealType"] = dealType
        params["dealNum"] = dealNum
        params["dealAmount"] = dealAmount
        params["payPassword"] = payPassword
        params["coinCode"] = coinCode
        viewModelScope.safeLaunch {
            block = {
                val datas = coinCurrencyRepository.getAddAd(params)
                if (datas.code == 0) {
                    addAdData.value = datas.msg
                } else {
                    errorLiveData.value = datas.msg;
                }
            }
            onError = {
                //errorLiveData.value = it.message;
                doError(it)
            }
        }
    }

    /**
     * 撤单
     */
    fun cancellationOrders(adId: String) {
        val params = HashMap<String, Any>()
        params["adId"] = adId
        viewModelScope.safeLaunch {
            block = {
                val datas = coinCurrencyRepository.getAdDown(params)
                if (datas.code == 0) {
                    adDownData.value = datas.msg
                } else {
                    errorLiveData.value = datas.msg;
                }
            }
            onError = {
                //errorLiveData.value = it.message;
                doError(it)
            }
        }
    }
}